/*
 * @Title: 姿美汇H5商城打包文件
 * @Author: JSL
 * @Date:   2018-10-29
 * @Last Modified by:   JSL
 * @Last Modified time: 2018-10-29
 */
var gulp = require('gulp'),
  autoprefixer = require('gulp-autoprefixer'),
  minifycss = require('gulp-minify-css'), //压缩css
  jshint = require('gulp-jshint'), //js代码校验
  rev = require('gulp-rev-append'), // 插入文件指纹（MD5）
  babel = require('gulp-babel'), //es6转es5 181029卡一上午
  uglify = require('gulp-uglify'), //压缩JS
  imagemin = require('gulp-imagemin'), //压缩图片
  rename = require('gulp-rename'), //改名
  concat = require('gulp-concat'), // 合并文件
  notify = require('gulp-notify'), // 相当于 console.log()
  cache = require('gulp-cache'),
  livereload = require('gulp-livereload'),
  del = require('del'),
  htmlmin = require('gulp-htmlmin'),
  webserver = require('gulp-webserver');
  // obfuscate = require('gulp-obfuscate');//混淆压缩 效果不好

gulp.task('webserver', function() {
  gulp.src('./') //./代表根目录
    .pipe(webserver({
      port: 8090, //端口
      // host: '172.20.0.235', //自己的域名
      livereload: true, //实时刷新代码。有点问题 保存两次才生效
      directoryListing: {
        path: './',
        enable: true
      }
    }))
});

// Styles
gulp.task('css', function() { //仅此项注释，后续代码类似
  return gulp.src('css/*.css') //声明压缩的文件路径
    .pipe(minifycss()) //压缩文件
    .pipe(gulp.dest('dist/css')) //打包文件夹下的所有文件
    .pipe(notify({ //打印压缩文件成功信息
      message: 'css task complete'
    }));
});
// Scripts
gulp.task('js', function() {
  return gulp.src('js/*.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default'))
    .pipe(babel())
		// .pipe(obfuscate())
    .pipe(uglify())
    .pipe(gulp.dest('dist/js'))
    .pipe(notify({
      message: 'js task complete'
    }));
});
// Images
gulp.task('img', function() {
  return gulp.src('img/**/*')
    .pipe(cache(imagemin({
      optimizationLevel: 3,
      progressive: true,
      interlaced: true
    })))
    .pipe(gulp.dest('dist/img'))
    .pipe(notify({
      message: 'img task complete'
    }));
});
// font
gulp.task('font', function() {
  return gulp.src('font/*')
    .pipe(gulp.dest('dist/font'))
    .pipe(notify({
      message: 'font task complete'
    }));
});
// lisp
gulp.task('lisp', function() {
  return gulp.src('lisp/**/*')
    .pipe(gulp.dest('dist/lisp'))
    .pipe(notify({
      message: 'lisp task complete'
    }));
});
//html
gulp.task('html', function() {
  var options = {
    collapseWhitespace: true, //压缩HTML
    removeComments: true, //清除HTML注释
    removeEmptyAttributes: true, //删除所有空格作属性值
    minifyJS: true, //压缩页面JS
    minifyCSS: true //压缩页面JS
  };
  return gulp.src('html/*html')
    .pipe(rev())
    .pipe(htmlmin(options))
    .pipe(gulp.dest('dist/html'))
    .pipe(notify({
      message: 'html task complete'
    }));
});

// Clean  任务执行前，先清除之前生成的文件
gulp.task('clean', function(cb) {
  del(['dist/css', 'dist/font', 'dist/html', 'dist/img', 'dist/js', 'dist/lisp'], cb)
});

// 设置打包任务
gulp.task('pack', ['clean', 'css', 'js', 'img', 'font', 'lisp', 'html'], function() {
  gulp.start('css', 'js', 'img', 'font', 'lisp', 'html');
});

// Watch
gulp.task('watch', function() {
  // Watch .scss files
  gulp.watch('css/*.css', ['css']);
  // Watch .js files
  gulp.watch('js/*.js', ['js']);
  // Watch image files
  gulp.watch('img/**/*', ['img']);
  // Watch font files
  gulp.watch('font/*', ['font']);
  // Watch lisp files
  gulp.watch('lisp/**/*', ['lisp']);
  // Watch html files
  gulp.watch('html/*html', ['html']);
  // Create LiveReload server
  livereload.listen();
  // Watch any files in dist/, reload on change
  gulp.watch(['dist/**']).on('change', livereload.changed); //本地启动服务后修改自动刷新
  // gulp.watch('dist/**/*.*', function(file) {
  //   livereload.changed(file.path);
  // });
});
// 设置默认启本地服务并实时监听
gulp.task('default', ['webserver', 'watch']);
